package com.hskn.hss.module.tlthresholdtrans.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.tlthresholdtrans.entity.TlThresholdTrans;
import com.hskn.hss.module.tlthresholdtrans.mapper.TlThresholdTransMapper;
import com.hskn.hss.module.tlthresholdtrans.service.ITlThresholdTransService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-13
 */
@Service
public class TlThresholdTransServiceImpl extends ServiceImpl<TlThresholdTransMapper, TlThresholdTrans> implements ITlThresholdTransService {

    @Resource
    TlThresholdTransMapper tlThresholdTransMapper;

    @Override
    public AjaxResult insert(JSONObject params) throws HssException {

        TlThresholdTrans tlThresholdTrans = params.getObject("data", TlThresholdTrans.class);
        int i = tlThresholdTransMapper.insert(tlThresholdTrans);
        boolean flag = i > 0 ? true : false;

        if (flag) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    public AjaxResult update(JSONObject params) throws HssException {

        TlThresholdTrans tlThresholdTrans = params.getObject("data", TlThresholdTrans.class);
        int i = tlThresholdTransMapper.updateById(tlThresholdTrans);
        boolean flag = i > 0 ? true : false;
        if (flag) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    public TlThresholdTrans getDetail(JSONObject params) throws HssException {
        String id = params.getString("id");
        TlThresholdTrans tlThresholdTrans = tlThresholdTransMapper.selectById(id);
        return tlThresholdTrans;
    }

    @Override
    public String getTrans(String thcode, String num) {
        QueryWrapper<TlThresholdTrans> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TlThresholdTrans::getThresholdcode, thcode);
        TlThresholdTrans tlThresholdTrans = tlThresholdTransMapper.selectOne(queryWrapper);
        String result = tlThresholdTrans.getThresholdtrans() + num + tlThresholdTrans.getThresholdunit();
        return result;
    }
}
